<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>The source code</title>
  <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  <style type="text/css">
    .highlight { display: block; background-color: #ddd; }
  </style>
  <script type="text/javascript">
    function highlight() {
      document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
    }
  </script>
</head>
<body onload="prettyPrint(); highlight();">
  <pre class="prettyprint lang-js"><span id='Ext-direct-PollingProvider'>/**
</span> * @class Ext.direct.PollingProvider
 * @extends Ext.direct.JsonProvider
 *
 * &lt;p&gt;Provides for repetitive polling of the server at distinct {@link #interval intervals}.
 * The initial request for data originates from the client, and then is responded to by the
 * server.&lt;/p&gt;
 *
 * &lt;p&gt;All configurations for the PollingProvider should be generated by the server-side
 * API portion of the Ext.Direct stack.&lt;/p&gt;
 *
 * &lt;p&gt;An instance of PollingProvider may be created directly via the new keyword or by simply
 * specifying &lt;tt&gt;type = 'polling'&lt;/tt&gt;.  For example:&lt;/p&gt;
 * &lt;pre&gt;&lt;code&gt;
var pollA = new Ext.direct.PollingProvider({
    type:'polling',
    url: 'php/pollA.php',
});
Ext.Direct.addProvider(pollA);
pollA.disconnect();

Ext.Direct.addProvider(
    {
        type:'polling',
        url: 'php/pollB.php',
        id: 'pollB-provider'
    }
);
var pollB = Ext.Direct.getProvider('pollB-provider');
 * &lt;/code&gt;&lt;/pre&gt;
 */
Ext.direct.PollingProvider = Ext.extend(Ext.direct.JsonProvider, {
<span id='Ext-direct-PollingProvider-cfg-priority'>    /**
</span>     * @cfg {Number} priority
     * Priority of the request (defaults to &lt;tt&gt;3&lt;/tt&gt;). See {@link Ext.direct.Provider#priority}.
     */
<span id='Ext-direct-PollingProvider-cfg-priority'>    // override default priority
</span>    priority: 3,

<span id='Ext-direct-PollingProvider-cfg-interval'>    /**
</span>     * @cfg {Number} interval
     * How often to poll the server-side in milliseconds (defaults to &lt;tt&gt;3000&lt;/tt&gt; - every
     * 3 seconds).
     */
    interval: 3000,

<span id='Ext-direct-PollingProvider-cfg-baseParams'>    /**
</span>     * @cfg {Object} baseParams An object containing properties which are to be sent as parameters
     * on every polling request
     */

<span id='Ext-direct-PollingProvider-cfg-url'>    /**
</span>     * @cfg {String/Function} url
     * The url which the PollingProvider should contact with each request. This can also be
     * an imported Ext.Direct method which will accept the baseParams as its only argument.
     */

<span id='Ext-direct-PollingProvider-method-constructor'>    // private
</span>    constructor : function(config){
        Ext.direct.PollingProvider.superclass.constructor.call(this, config);
        this.addEvents(
<span id='Ext-direct-PollingProvider-event-beforepoll'>            /**
</span>             * @event beforepoll
             * Fired immediately before a poll takes place, an event handler can return false
             * in order to cancel the poll.
             * @param {Ext.direct.PollingProvider} this
             */
            'beforepoll',
<span id='Ext-direct-PollingProvider-event-poll'>            /**
</span>             * @event poll
             * This event has not yet been implemented.
             * @param {Ext.direct.PollingProvider} this
             */
            'poll'
        );
    },

<span id='Ext-direct-PollingProvider-method-isConnected'>    // inherited
</span>    isConnected: function(){
        return !!this.pollTask;
    },

<span id='Ext-direct-PollingProvider-method-connect'>    /**
</span>     * Connect to the server-side and begin the polling process. To handle each
     * response subscribe to the data event.
     */
    connect: function(){
        if(this.url &amp;&amp; !this.pollTask){
            this.pollTask = Ext.TaskMgr.start({
                run: function(){
                    if(this.fireEvent('beforepoll', this) !== false){
                        if(typeof this.url == 'function'){
                            this.url(this.baseParams);
                        }else{
                            Ext.Ajax.request({
                                url: this.url,
                                callback: this.onData,
                                scope: this,
                                params: this.baseParams
                            });
                        }
                    }
                },
                interval: this.interval,
                scope: this
            });
            this.fireEvent('connect', this);
        }else if(!this.url){
            throw 'Error initializing PollingProvider, no url configured.';
        }
    },

<span id='Ext-direct-PollingProvider-method-disconnect'>    /**
</span>     * Disconnect from the server-side and stop the polling process. The disconnect
     * event will be fired on a successful disconnect.
     */
    disconnect: function(){
        if(this.pollTask){
            Ext.TaskMgr.stop(this.pollTask);
            delete this.pollTask;
            this.fireEvent('disconnect', this);
        }
    },

<span id='Ext-direct-PollingProvider-method-onData'>    // private
</span>    onData: function(opt, success, xhr){
        if(success){
            var events = this.getEvents(xhr);
            for(var i = 0, len = events.length; i &lt; len; i++){
                var e = events[i];
                this.fireEvent('data', this, e);
            }
        }else{
            var e = new Ext.Direct.ExceptionEvent({
                data: e,
                code: Ext.Direct.exceptions.TRANSPORT,
                message: 'Unable to connect to the server.',
                xhr: xhr
            });
            this.fireEvent('data', this, e);
        }
    }
});

Ext.Direct.PROVIDERS['polling'] = Ext.direct.PollingProvider;</pre>
</body>
</html>
